package com.ncp.model.biz.registerlogin;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.ncp.model.dao.DB;

import com.ncp.model.dao.DbConnection;
import com.ncp.model.entity.registerlogin.UserBasicBean;

import common.util.security.EncryptUtil;

public class LoginBiz {
	/*
	 * 判断是否已经为注册用户
	 * */
	private DB common=new DB();
	private PreparedStatement pstmt=null;
	private Connection conn=null;
	private ResultSet rs=null;
	
	public UserBasicBean isRegisterUser(String userLoginName){
		UserBasicBean ubb=new UserBasicBean();
		/**
		 * 根据不同的返回值，判断对应的状态信息
		 * -1代表不是注册用户。
		 * 1代表用户是注册用户 
		 */
		String sql = "select * from USERBASIC where UserLoginName='"+userLoginName+"'";		
		try {
			conn=DbConnection.getConnByDataSource();
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			if(rs.next()){
				ubb.setAddress(rs.getString("Address"));
				ubb.setEmail(rs.getString("Email"));
				ubb.setLevelID(rs.getInt("LevelID"));
				ubb.setPassWord(rs.getString("PassWord"));
				ubb.setUserImg(rs.getString("UserImage"));
				//ubb.setRegisterDate(rs.getDate("RegisterDate")+"");
				ubb.setStatus(rs.getString("Status"));
				ubb.setTel(rs.getString("Tel"));
				ubb.setUserLoginName(rs.getString("UserLoginName"));
				ubb.setUserName(rs.getString("UserName"));
				ubb.setUserID(rs.getInt("UserID"));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			common.closeSQL(conn, pstmt);
		}
		return ubb;
	}
	/**
	 * 判断用户密码是否正确
	 * @param userLoginName
	 * @param passWord
	 * @return
	 */
	public int checkUserPsd(String password,String inputpassWord){
		/***判断用户密码是否输入正确
		 * -1代表密码错误。
		 * 1代表密码输入正确****/
		int result = -1;
			EncryptUtil encryutil = new EncryptUtil();
			String Shapassword = encryutil.sha(inputpassWord);
			if(Shapassword.equals(password))
			{
				result = 1;
			}
			else
			{
				result = -1;
			}
		return result;
	}
		
	/**
	 * 判断用户是否为审批用户	
	 * @param userLoginName
	 * @return 
	 */
	public int isApprovedUser(String Status){
		/***根据不同的返回值，判断对应的状态信息
		 * -1代表不是审批用户。
		 * 1代表用户是审批用户. ****/
		int result = -1;
	    if(Status.equals("true")){
			result=1;
		}else{ 
			result=-1;
				}
		return result;
	}
	
	public boolean delImgsInTomcat(int UserID,String url){
		boolean b=false;
		
		String sql = "select UserImage from USERBASIC where UserID="+UserID;
		common.setSql(sql);
		String UserImage = common.getProductItem();
		

		File file = new File(url+UserImage);
		if(file.exists()){
		  b = file.delete();
		}
		
		return b;
	}
	public boolean delImgsInTomcatBef(int UserID,String url){
		boolean b=false;
		
		String sql = "select UserImageBef from USERBASIC where UserID="+UserID;
		common.setSql(sql);
		String UserImage = common.getProductItem();
		File file = new File(url+UserImage);
		if(file.exists()){
		  b = file.delete();
		}
		
		return b;
	}
	
	public boolean setMyImgBef(int UserID,String url){
		boolean b = false;
		
		String sql = "update USERBASIC set UserImageBef="+"'"+url+"' where UserID="+UserID;
		try {
			conn=DbConnection.getConnByDataSource();
			pstmt=conn.prepareStatement(sql);
			int i=pstmt.executeUpdate();
			if(i==1){
				b=true;
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			common.closeSQL(conn, pstmt);
		}
		return b;
		
		
	}
	public boolean setMyImg(int UserID,String url){
		boolean b = false;
		
		String sql = "update USERBASIC set UserImage="+"'"+url+"' where UserID="+UserID;
		try {
			conn=DbConnection.getConnByDataSource();
			pstmt=conn.prepareStatement(sql);
			int i=pstmt.executeUpdate();
			if(i==1){
				b=true;
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			common.closeSQL(conn, pstmt);
		}
		return b;
		
		
	}
	
	public UserBasicBean getMyImg(int UserID){
		String sql = "select * from USERBASIC where  UserID="+UserID;
		UserBasicBean ubb=new UserBasicBean();
		try {
			conn=DbConnection.getConnByDataSource();
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			if(rs.next()){
				ubb.setLevelID(rs.getInt("LevelID"));
			    ubb.setUserImg(rs.getString("UserImage"));
				ubb.setUserLoginName(rs.getString("UserLoginName"));
				ubb.setUserID(rs.getInt("UserID"));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			common.closeSQL(conn, pstmt);
		}
		return ubb;
		
		
	}
}
